﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Windows.Media.Imaging;
using DiamondDash.Core;
using DiamondDash.Pages;

namespace DiamondDash.Views
{
    public partial class ToolSelector : UserControl
    {
        /// <summary>
        /// 道具类型
        /// 1-炸弹 2-时间 3-调色板
        /// </summary>
        public int ToolType { get; set; }

        public int CurrCost { get; set; }


        public const string bombImageUri = "/Images/1_029.png";
        public const string timeImageUri = "/Images/time_and_date.png";
        public const string colorImageUri = "/Images/Color palette.png";

        public ToolSelector()
        {
            InitializeComponent();
            this.Loaded += ToolSelector_Loaded;
            this.btnAdd.Click += btnAdd_Click;
        }

        public void Init()
        {
            //根据类型进行展示
            CurrCost = 0;
            switch (ToolType)
            {
                case 1:
                    CurrCost = GameStatics.BombCoinCost;
                    this.txtToolName.Text = "SUPER BOMB";
                    break;
                case 2:
                    CurrCost = GameStatics.TimeCoinCost;
                    this.txtToolName.Text = "TIME";
                    break;
                case 3:
                    CurrCost = GameStatics.ColorCoinCost;
                    this.txtToolName.Text = "COLOR PAINT";
                    break;
            }
            this.txtCoin.Text = CurrCost.ToString();
        }

        void ToolSelector_Loaded(object sender, RoutedEventArgs e)
        {
            this.Loaded -= ToolSelector_Loaded;
            string uri = string.Empty;
            switch (ToolType)
            {
                case 1:
                    uri = bombImageUri;
                    break;
                case 2:
                    uri = timeImageUri;
                    break;
                case 3:
                    uri = colorImageUri;
                    break;
            }
            BitmapImage bitmap = new BitmapImage(new Uri(uri, UriKind.RelativeOrAbsolute));
            imgTool.Source = bitmap;
        }

        void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            SoundManager.PlayEffect(SoundManager.clickButton);
            //购买道具
            if (StageManager.Instance.TotalG >= CurrCost)
            {
                //购买
                StageManager.Instance.TotalG -= CurrCost;
                switch (ToolType)
                {
                    case 1:
                        StageManager.Instance.BombCount++;
                        break;
                    case 2:
                        StageManager.Instance.TimeCount++;
                        break;
                    case 3:
                        StageManager.Instance.ColorCount++;
                        break;
                }
                //通知更新界面
                ReadyPage.Instance.RefreshUI();
            }
            else
            {
                MessageBox.Show("You don't have enough coin.");
            }
        }

        public void SetCount(int count)
        {
            this.txtCount.Text = count.ToString();
        }

        internal void SetLock(bool isLock, string unlockInfo = "")
        {
            this.btnAdd.IsEnabled = !isLock;
            if (isLock)
            {
                this.bdrCount.Visibility = System.Windows.Visibility.Collapsed;
                this.bdrLock.Visibility = System.Windows.Visibility.Visible;
                this.txtCoin.Text = unlockInfo;
            }
            else
            {
                this.bdrCount.Visibility = System.Windows.Visibility.Visible;
                this.bdrLock.Visibility = System.Windows.Visibility.Collapsed;
                this.txtCoin.Text = CurrCost.ToString();
            }
        }
    }


}
